package com.emm.yixun.mobile.ui.web

import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.provider.ContactsContract
import com.emm.yixun.mobile.R
import com.emm.yixun.mobile.application.EmmApplication
import java.io.ByteArrayOutputStream

object Util {

    fun getCommonParam(): Map<String, String> {
        return mutableMapOf<String, String>().apply {
            put("token", EmmApplication.getToken())
            put("userId", EmmApplication.getUserId())
            put("terminalCode", EmmApplication.getPackageManager(2))
            put("merchantId", EmmApplication.getData(EmmApplication.merchantId))
            put("projectId", EmmApplication.getProjectID())
            put("projectCode", EmmApplication.getProjectCode())
            put("version", EmmApplication.getPackageManager(5))
        }
    }

    fun insertUser2Contacts(context: Context, name: String, tel: String): Boolean {
        try {
            val values = ContentValues()
            val contentResolver = context.applicationContext.contentResolver

            // 下面的操作会根据RawContacts表中已有的rawContactId使用情况自动生成新联系人的rawContactId
            val rawContactUri = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values)
            val rawContactId = ContentUris.parseId(rawContactUri)

            // 向data表插入姓名数据
            if (name.isNotEmpty()) {
                values.clear()
                values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId)
                values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name)
                contentResolver.insert(ContactsContract.Data.CONTENT_URI, values)
            }

            // 向data表插入电话数据
            if (tel.isNotEmpty()) {
                values.clear()
                values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId)
                values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, tel)
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                contentResolver.insert(ContactsContract.Data.CONTENT_URI, values)
            }

            // 向data表插入头像数据
            val sourceBitmap = BitmapFactory.decodeResource(context.resources, R.drawable.logo180)
            val os = ByteArrayOutputStream()
            // 将Bitmap压缩成PNG编码，质量为100%存储
            sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os)
            val avatar = os.toByteArray()
            values.put(ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId)
            values.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
            values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, avatar)
            contentResolver.insert(ContactsContract.Data.CONTENT_URI, values)
        } catch (e: Exception) {
            return false
        }

        return true
    }

}